package com.grace.service;

import com.google.common.base.Preconditions;
import com.grace.beans.PageQuery;
import com.grace.beans.PageResult;
import com.grace.common.RequestHolder;
import com.grace.dao.SysAclMapper;
import com.grace.exception.ParamException;
import com.grace.model.SysAcl;
import com.grace.param.AclParam;
import com.grace.util.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SysAclService {

    @Autowired
    private SysAclMapper sysAclMapper;

    @Autowired
    private SysLogService sysLogService;

    /**
     * 保存
     */
    public void save(AclParam param) {
        //校验对象
        BeanValidator.check(param);
        if (checkExist(param.getAclModuleId(), param.getName(), param.getId())) {
            throw new ParamException("当前权限模块下存在下相同名称的权限点");
        }
        //构建对象
        SysAcl sysAcl = SysAcl.builder().name(param.getName()).aclModuleId(param.getAclModuleId())
                .url(param.getUrl()).type(param.getType()).status(param.getStatus())
                .seq(param.getSeq()).remark(param.getRemark()).build();
        //调用: 简单生成唯一编码的方法
        sysAcl.setCode(generateCode());
        sysAcl.setOperator(RequestHolder.getCurrentUser().getUsername());
        sysAcl.setOperatorTime(new Date());

        //插入数据库
        sysAclMapper.insertSelective(sysAcl);

        //记录日志
        sysLogService.saveAclLog(null, sysAcl);
    }

    /**
     * 更新
     */
    public void update(AclParam param) {
        //校验对象
        BeanValidator.check(param);
        if (checkExist(param.getAclModuleId(), param.getName(), param.getId())) {
            throw new ParamException("当前权限模块下存在下相同的权限点");
        }
        //根据id判断权限点是否存在
        SysAcl before = sysAclMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before, "待更新的权限点不存在");

        //构建对象
        SysAcl after = SysAcl.builder().id(param.getId()).name(param.getName()).aclModuleId(param.getAclModuleId())
                .url(param.getUrl()).type(param.getType()).status(param.getStatus())
                .seq(param.getSeq()).remark(param.getRemark()).build();

        after.setOperator(RequestHolder.getCurrentUser().getUsername());
        after.setOperatorTime(new Date());

        //更新数据
        sysAclMapper.updateByPrimaryKeySelective(after);

        //记录日志
        sysLogService.saveAclLog(before, after);


    }

    /**
     * 判断当前权限模块下是否存在下相同的权限点
     */
    public boolean checkExist(int aclModuleId, String name, Integer id) {

        return sysAclMapper.countByNameAndAclModuleId(aclModuleId, name, id) > 0;
    }

    /**
     * 简单生成唯一编码的方法
     */
    public String generateCode() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(new Date()) + "_" + (int) (Math.random() * 100);
    }

    /**
     * 分页的方法
     */
    public PageResult<SysAcl> getPageByAclModuleId(int aclModuleId, PageQuery page) {
        //对参数进行校验
        BeanValidator.check(page);
        //查询一下总条数
        int count = sysAclMapper.countByAclModuleId(aclModuleId);
        if (count > 0) {
            List<SysAcl> list = sysAclMapper.getPageByAclModuleId(aclModuleId, page);
            return PageResult.<SysAcl>builder().total(count).data(list).build();
        }
        return PageResult.<SysAcl>builder().build();
    }
}
